﻿namespace Hidistro.ControlPanel.Sales
{
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Core.Enums;
    using Hidistro.Entities;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Sales;
    using Hidistro.SqlDal.Sales;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public sealed class SalesHelper
    {
        private SalesHelper()
        {
        }

        public static bool AddExpressTemplate(string expressName, string xmlFile)
        {
            return new ExpressTemplateDao().AddExpressTemplate(expressName, xmlFile);
        }

        public static bool AddShipper(ShippersInfo shipper)
        {
            Globals.EntityCoding(shipper, true);
            return new ShipperDao().AddShipper(shipper);
        }

        public static decimal CalcFreight(int regionId, int totalWeight, ShippingModeInfo shippingModeInfo)
        {
            decimal price = 0M;
            int topRegionId = RegionHelper.GetTopRegionId(regionId);
            decimal num3 = totalWeight;
            decimal num4 = 1M;
            if ((num3 > shippingModeInfo.Weight) && (shippingModeInfo.AddWeight.HasValue && (shippingModeInfo.AddWeight.Value > 0M)))
            {
                decimal num6 = num3 - shippingModeInfo.Weight;
                if ((num6 % shippingModeInfo.AddWeight) == 0M)
                {
                    num4 = (num3 - shippingModeInfo.Weight) / shippingModeInfo.AddWeight.Value;
                }
                else
                {
                    num4 = 1 + ((num3 - shippingModeInfo.Weight) / shippingModeInfo.AddWeight.Value);
                }
            }
            if ((shippingModeInfo.ModeGroup == null) || (shippingModeInfo.ModeGroup.Count == 0))
            {
                if ((num3 > shippingModeInfo.Weight) && shippingModeInfo.AddPrice.HasValue)
                {
                    return ((num4 * shippingModeInfo.AddPrice.Value) + shippingModeInfo.Price);
                }

                ShoppingCartInfo shoping = new ShoppingCartInfo();
                decimal me = shoping.GetTotal();
                if (me >= 199)
                {
                    shippingModeInfo.Price = shippingModeInfo.Price - shippingModeInfo.Price;
                }
                
                return shippingModeInfo.Price;
            }
            int? nullable = null;
            foreach (ShippingModeGroupInfo info in shippingModeInfo.ModeGroup)
            {
                foreach (ShippingRegionInfo info2 in info.ModeRegions)
                {
                    if (topRegionId == info2.RegionId)
                    {
                        nullable = new int?(info2.GroupId);
                        break;
                    }
                }
                if (nullable.HasValue)
                {
                    if (num3 > shippingModeInfo.Weight)
                    {
                        price = (num4 * info.AddPrice) + info.Price;
                    }
                    else
                    {
                        price = info.Price;
                    }
                    break;
                }
            }
            if (nullable.HasValue)
            {
                return price;
            }
            if ((num3 > shippingModeInfo.Weight) && shippingModeInfo.AddPrice.HasValue)
            {
                return ((num4 * shippingModeInfo.AddPrice.Value) + shippingModeInfo.Price);
            }
            ShoppingCartInfo shopings = new ShoppingCartInfo();
            decimal mes = shopings.GetTotal();
            if (mes >= 199)
            {
                shippingModeInfo.Price = shippingModeInfo.Price - shippingModeInfo.Price;
            }
            return shippingModeInfo.Price;
        }

        public static PaymentModeActionStatus CreatePaymentMode(PaymentModeInfo paymentMode)
        {
            if (null == paymentMode)
            {
                return PaymentModeActionStatus.UnknowError;
            }
            Globals.EntityCoding(paymentMode, false);
            return new PaymentModeDao().CreateUpdateDeletePaymentMode(paymentMode, DataProviderAction.Create);
        }

        public static bool CreateShippingMode(ShippingModeInfo shippingMode)
        {
            if (null == shippingMode)
            {
                return false;
            }
            return new ShippingModeDao().CreateShippingMode(shippingMode);
        }

        public static bool CreateShippingTemplate(ShippingModeInfo shippingMode)
        {
            return new ShippingModeDao().CreateShippingTemplate(shippingMode);
        }

        public static bool DeleteExpressTemplate(int expressId)
        {
            return new ExpressTemplateDao().DeleteExpressTemplate(expressId);
        }

        public static bool DeletePaymentMode(int modeId)
        {
            PaymentModeInfo paymentMode = new PaymentModeInfo {
                ModeId = modeId
            };
            return (new PaymentModeDao().CreateUpdateDeletePaymentMode(paymentMode, DataProviderAction.Delete) == PaymentModeActionStatus.Success);
        }

        public static bool DeleteShipper(int shipperId)
        {
            return new ShipperDao().DeleteShipper(shipperId);
        }

        public static bool DeleteShippingMode(int modeId)
        {
            return new ShippingModeDao().DeleteShippingMode(modeId);
        }

        public static bool DeleteShippingTemplate(int templateId)
        {
            return new ShippingModeDao().DeleteShippingTemplate(templateId);
        }

        public static DataTable GetDaySaleTotal(int year, int month, SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetDaySaleTotal(year, month, saleStatisticsType);
        }

        public static decimal GetDaySaleTotal(int year, int month, int day, SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetDaySaleTotal(year, month, day, saleStatisticsType);
        }

        public static IList<string> GetExpressCompanysByMode(int modeId)
        {
            return new ShippingModeDao().GetExpressCompanysByMode(modeId);
        }

        public static DataTable GetExpressTemplates()
        {
            return new ExpressTemplateDao().GetExpressTemplates(null);
        }

        public static DataTable GetIsUserExpressTemplates()
        {
            return new ExpressTemplateDao().GetExpressTemplates(true);
        }

        public static DataTable GetMemberStatistics(SaleStatisticsQuery query, out int totalProductSales)
        {
            return new SaleStatisticDao().GetMemberStatistics(query, out totalProductSales);
        }

        public static DataTable GetMemberStatisticsNoPage(SaleStatisticsQuery query)
        {
            return new SaleStatisticDao().GetMemberStatisticsNoPage(query);
        }

        public static DataTable GetMonthSaleTotal(int year, SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetMonthSaleTotal(year, saleStatisticsType);
        }

        public static decimal GetMonthSaleTotal(int year, int month, SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetMonthSaleTotal(year, month, saleStatisticsType);
        }

        public static PaymentModeInfo GetPaymentMode(int modeId)
        {
            return new PaymentModeDao().GetPaymentMode(modeId);
        }

        public static PaymentModeInfo GetPaymentMode(string gateway)
        {
            return new PaymentModeDao().GetPaymentMode(gateway);
        }

        public static IList<PaymentModeInfo> GetPaymentModes(PayApplicationType payApplicationType)
        {
            return new PaymentModeDao().GetPaymentModes(payApplicationType);
        }

        public static DataTable GetProductSales(SaleStatisticsQuery productSale, out int totalProductSales)
        {
            if (productSale == null)
            {
                totalProductSales = 0;
                return null;
            }
            return new SaleStatisticDao().GetProductSales(productSale, out totalProductSales);
        }

        public static DataTable GetProductSalesNoPage(SaleStatisticsQuery productSale, out int totalProductSales)
        {
            if (productSale == null)
            {
                totalProductSales = 0;
                return null;
            }
            return new SaleStatisticDao().GetProductSalesNoPage(productSale, out totalProductSales);
        }

        public static DataTable GetProductVisitAndBuyStatistics(SaleStatisticsQuery query, out int totalProductSales)
        {
            return new SaleStatisticDao().GetProductVisitAndBuyStatistics(query, out totalProductSales);
        }

        public static DataTable GetProductVisitAndBuyStatisticsNoPage(SaleStatisticsQuery query, out int totalProductSales)
        {
            return new SaleStatisticDao().GetProductVisitAndBuyStatisticsNoPage(query, out totalProductSales);
        }

        public static DbQueryResult GetSaleOrderLineItemsStatistics(SaleStatisticsQuery query)
        {
            return new SaleStatisticDao().GetSaleOrderLineItemsStatistics(query);
        }

        public static DbQueryResult GetSaleOrderLineItemsStatisticsNoPage(SaleStatisticsQuery query)
        {
            return new SaleStatisticDao().GetSaleOrderLineItemsStatisticsNoPage(query);
        }

        public static DbQueryResult GetSaleTargets()
        {
            return new SaleStatisticDao().GetSaleTargets();
        }

        public static ShippersInfo GetShipper(int shipperId)
        {
            return new ShipperDao().GetShipper(shipperId);
        }

        public static IList<ShippersInfo> GetShippers(bool includeDistributor)
        {
            return new ShipperDao().GetShippers(includeDistributor);
        }

        public static DataTable GetShippingAllTemplates()
        {
            return new ShippingModeDao().GetShippingAllTemplates();
        }

        public static ShippingModeInfo GetShippingMode(int modeId, bool includeDetail)
        {
            return new ShippingModeDao().GetShippingMode(modeId, includeDetail);
        }

        public static ShippingModeInfo GetShippingModeByCompany(string companyname)
        {
            return new ShippingModeDao().GetShippingModeByCompany(companyname);
        }

        public static IList<ShippingModeInfo> GetShippingModes()
        {
            return new ShippingModeDao().GetShippingModes();
        }

        public static ShippingModeInfo GetShippingTemplate(int templateId, bool includeDetail)
        {
            return new ShippingModeDao().GetShippingTemplate(templateId, includeDetail);
        }

        public static DbQueryResult GetShippingTemplates(Pagination pagin)
        {
            return new ShippingModeDao().GetShippingTemplates(pagin);
        }

        public static AdminStatisticsInfo GetStatistics()
        {
            return new SaleStatisticDao().GetStatistics();
        }

        public static IList<UserStatisticsForDate> GetUserAdd(int? year, int? month, int? days)
        {
            return new DateStatisticDao().GetUserAdd(year, month, days);
        }

        public static OrderStatisticsInfo GetUserOrders(OrderQuery userOrder)
        {
            return new SaleStatisticDao().GetUserOrders(userOrder);
        }

        public static OrderStatisticsInfo GetUserOrdersNoPage(OrderQuery userOrder)
        {
            return new SaleStatisticDao().GetUserOrdersNoPage(userOrder);
        }

        public static IList<UserStatisticsInfo> GetUserStatistics(Pagination page, out int totalProductSaleVisits)
        {
            if (page == null)
            {
                totalProductSaleVisits = 0;
                return null;
            }
            return new SaleStatisticDao().GetUserStatistics(page, out totalProductSaleVisits);
        }

        public static DataTable GetWeekSaleTota(SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetWeekSaleTota(saleStatisticsType);
        }

        public static decimal GetYearSaleTotal(int year, SaleStatisticsType saleStatisticsType)
        {
            return new DateStatisticDao().GetYearSaleTotal(year, saleStatisticsType);
        }

        public static void SetDefalutShipper(int shipperId)
        {
            new ShipperDao().SetDefalutShipper(shipperId);
        }


        public static bool SetExpressIsUse(int expressId)
        {
            return new ExpressTemplateDao().SetExpressIsUse(expressId);
        }

        public static void SwapPaymentModeSequence(int modeId, int replaceModeId, int displaySequence, int replaceDisplaySequence)
        {
            new PaymentModeDao().SwapPaymentModeSequence(modeId, replaceModeId, displaySequence, replaceDisplaySequence);
        }

        public static void SwapShippingModeSequence(int modeId, int replaceModeId, int displaySequence, int replaceDisplaySequence)
        {
            new ShippingModeDao().SwapShippingModeSequence(modeId, replaceModeId, displaySequence, replaceDisplaySequence);
        }

        public static bool UpdateExpressTemplate(int expressId, string expressName)
        {
            return new ExpressTemplateDao().UpdateExpressTemplate(expressId, expressName);
        }

        public static PaymentModeActionStatus UpdatePaymentMode(PaymentModeInfo paymentMode)
        {
            if (null == paymentMode)
            {
                return PaymentModeActionStatus.UnknowError;
            }
            Globals.EntityCoding(paymentMode, false);
            return new PaymentModeDao().CreateUpdateDeletePaymentMode(paymentMode, DataProviderAction.Update);
        }

        public static bool UpdateShipper(ShippersInfo shipper)
        {
            Globals.EntityCoding(shipper, true);
            return new ShipperDao().UpdateShipper(shipper);
        }

        public static bool UpdateShippingTemplate(ShippingModeInfo shippingMode)
        {
            return new ShippingModeDao().UpdateShippingTemplate(shippingMode);
        }

        public static bool UpdateShippMode(ShippingModeInfo shippingMode)
        {
            if (shippingMode == null)
            {
                return false;
            }
            Globals.EntityCoding(shippingMode, true);
            return new ShippingModeDao().UpdateShippingMode(shippingMode);
        }
    }
}

